package com.ce.pms.common;

import org.junit.AfterClass;
import org.junit.BeforeClass;

import com.ce.common.db.base.DBConnection;
import com.ce.common.db.base.Database;
import com.ce.common.db.base.JDBCConnection;
import com.ce.common.db.base.PropertiesUtil;
import com.ce.common.db.execution.SQLFileExecution;
import com.ce.common.db.execution.impl.MySQLFileExecution;
import com.ce.common.db.handler.TableHandler;
import com.ce.common.db.handler.impl.MySQLTableHandler;
import com.ce.common.db.loader.DatabaseLoader;
import com.ce.common.db.loader.impl.DatabaseLoaderImpl;
/**
 * 功能描述: 数据库初始化功能处理类
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  胡煦
 * Create on:2008-12-22
 * Edit author:
 * Edit on:
 * 修改原因：

 */

public class PMSSuit {

	static TableHandler handler;

	static SQLFileExecution execution;

	static DatabaseLoader loader;

	static Database db;

	static DBConnection conn;
	@BeforeClass
	public static void setUpForDB() throws Exception {
		String osTypeStr = System.getProperty("os.name").toString().toUpperCase();

		db = new Database(PropertiesUtil.JDBC_USERNAME, PropertiesUtil.JDBC_PASSWD,
				PropertiesUtil.JDBC_DATABASE, PropertiesUtil.JDBC_HOST, "UTF8");
		conn = JDBCConnection.getJDBCConnection();
		handler = new MySQLTableHandler(conn);
		loader = new DatabaseLoaderImpl(conn);
		execution = new MySQLFileExecution(db);

		//执行初始化文件前, 先将整个库的表删除
		handler.dropTables(loader.getAllTables(new String[]{"TABLE", "VIEW"}));
		//判断操作系统
		if(osTypeStr.startsWith("WINDOWS")){
			execution.executeFile("src/test/resources/test_init.sql");
		}else{
			execution.executeFileInLinux("src/test/resources/test_init.sql");
		}

	}

	@AfterClass
	public static void tearDownForDB() throws Exception {
		db = null;
		conn = null;
		handler = null;
		loader = null;
		execution = null;
	}
}
